//
//  SinaStatusViewModel.swift
//  mySina
//
//  Created by 刘莹 on 2016/11/17.
//  Copyright © 2016年 liuying. All rights reserved.
//

import UIKit

class SinaStatusViewModel: NSObject {
    //MARK: -
    //MARK: - 属性
    //增加一个纯种的URL类型的属性--> 记录用户头像地址
    //用户头像
    var iconURL: URL?
    //等级图片
    var verifitedImage: UIImage?
    //
    var mbRankImage: UIImage?
    //微博来源
    var sourceText:String?
    //计算型属性
    var timeText:String?{
        
        //1.实例化日期格式化对象
        let dateFormater = DateFormatter()
        //2.指定格式化符
        dateFormater.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
        //在真机上必须设置本地化信息否则转换失败
        dateFormater.locale = Locale(identifier: "en")
        //3.将字符串转换日期对象
        //print("日期:\(status?.created_at)")
        guard let sinaDate = dateFormater.date(from: status?.created_at ?? "") else {
            return "来自火星"
        }
        //print(sinaDate)
        //4.处理日期
        //4.1判断是否是今年  Calendar提供非常丰富的日期处理函数
        let calendar = Calendar.current
        let currentDate = Date()
        let result = calendar.dateComponents([.day,.year], from: sinaDate, to: currentDate)
        if result.year == 0{
            //今年
            //if calendar.isDateInToday(currentDate){
            if calendar.isDateInToday(sinaDate){
                //今天
                //获取当前时间和sinaDate秒的差值
                let delta = currentDate.timeIntervalSince(sinaDate)
                if delta < 60{
                    return "刚刚"
                }else if delta < 3600{
                    return "\(Int(delta / 60))分钟前"
                }else{
                    return "\(Int(delta / 3600))小时前"
                }
                
                
            }else if calendar.isDateInYesterday(currentDate){
                //昨天
                dateFormater.dateFormat = "昨天 HH:mm"
                return dateFormater.string(from : sinaDate)
            }else{
                
                //其它 MM-dd HH:mm
                dateFormater.dateFormat = "MM-dd HH:mm"
                return dateFormater.string(from: sinaDate)
                
            }
            
        }else{
            
            //            往年
            dateFormater.dateFormat = "yyyy-MM-dd HH:mm"
            return dateFormater.string(from: sinaDate)
        }
        //5.重写设置格式化符
        
        //6.将日期对象转换对应格式的字符串
        return ""
        
    }
    
    var status: SinaStatus? {
        didSet {
            //一旦外界设置值 就应该到didSet中计算用户地址
            iconURL = URL(string: status?.user?.avatar_large ?? "")
            
            
            //处理微博认证的图片
            self.dealWeiboVerifitedImage()
            //处理微博等级图片
            self.dealMbrankImage()
            
            //self.timeText = dealWeiboDate(str: status?.create_at ?? "")
            self.sourceText = dealWeiboSource(source: status?.source ?? "")
        }
    }
//    //MARK: -
//    //MARK: - 微博日期
//    private func dealWeiboDate(str: String) -> String {
//        
//        //Sat Nov 19 11:28:03 +0800 2016
//        //let dateString = "Sat Nov 19 11:28:03 +0800 2016"//测试日期
//        //先将字符串处理成日期对象
//      
//    }
    

    //计算型属性,判断应该返回什么类型微博的pic_urls
    //MARK: -
    //MARK: - 微博图片
    var imageURLs: [SinaPictureInfo]? {
        if status?.retweeted_status == nil{
            //原创微博
            return status?.pic_urls
        }
        //转发微博
        return  status?.retweeted_status?.pic_urls
    }
    //MARK: -
    //MARK: - 1.等级图片
    private func dealMbrankImage() {
        let mbrank = status?.user?.mbrank ?? 0
        if mbrank > 0 && mbrank < 7 {
            //赋值
            mbRankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        
    }
    //处理数据相关的业务逻辑,处理微博日期,微博来源
    //MARK: -
    //MARK: - 2.等级图片
    private func dealWeiboVerifitedImage() {
        //微博认证的图片
        let type = status?.user?.verified_type ?? -1
        //判断type
        switch type {
        case 0:
            verifitedImage = #imageLiteral(resourceName: "avatar_vip")
        case 2,3,5:
            verifitedImage = #imageLiteral(resourceName: "avatar_enterprise_vip")
        case 220:
            verifitedImage = #imageLiteral(resourceName: "avatar_grassroot")
        default:
            //没有认证
            verifitedImage = nil
        }
    }
    //MARK: -
    //MARK: - 处理微博来源
    private func dealWeiboSource(source:String) ->String{
        //<a href=\"http://app.weibo.com/t/feed/6vtZb0\" rel=\"nofollow\">iPhone 6s 土豪金</a>
        let startFlag = "\">"
        let endFlag = "</a>"
        guard let startRange = source.range(of: startFlag),let endRange = source.range(of: endFlag) else{
                return "来自火星"
            }
        //uppperBound 最大边界  lowerBound:最小边界
        let range = startRange.upperBound..<endRange.lowerBound
        let subStr = "来自" + source.substring(with: range)
        return subStr
        }
   

}
